Ask a Question
Send an email to me.
 
Java Question and Answer Forums
JavaRanch Big Moose Saloon
Marcus Green's Discussion Forum
java.sun.com Forums, Chat and User Groups
Artima Java Answers Forum
 
Other Resources
Java Language Specification
Java Virtual Machine Specification
Java 2 Platform, Standard Edition, v 1.4.0 API Specification
 
Tutorials
Learning the Java Language
Operator Precedence Chart, Expressions, Statements, Blocks
Programming with Assertions
 
Answers: Certified Java Programmer Mock Exam
No.AnswerRemark
1b  Prints: 2  Java evaluates expressions from left to right while respecting operator precedence. (1 + 2 | 1 << 2 ^ 1 | 1), ((1 + 2) | ((1 << 2) ^ 1) | 1), ((3) | ((1 << 2) ^ 1) | 1), ((3) | ((4) ^ 1) | 1), ((3) | (5) | 1 == 7), (7%5=2)  
2c  Prints: 3  Java evaluates expressions from left to right while respecting operator precedence. (1 | 2 ^ 3 >> 2 & 14 + 2), (1 | (2 ^ ((3 >> 2) & (14 + 2))), (1 | (2 ^ ((0) & (16))), (1 | (2 ^ (0)), (1 | 2), (3),  
3a  Prints: [a,0],[b,1],1,0,0,2  The array index expression is evaluated before the right hand operand of the simple assignment operator. Evaluation of the array index expression causes method m to be invoked. The input parameter is the postfix increment expression with the operand i. Since the postfix increment expression returns the original value of the operand, the value zero is passed to method m along with a String containing the character a. Method m returns the value that was passed in as an input parameter. In this case, the return value is zero. As a side effect of the postfix increment expression, the value of variable i is one after the array index expression is evaluated. The right hand operand of the simple assignment operator is the return value of method m. The input parameter to method m is the postfix increment expression with variable i as the operand. The value of variable i is now one and that is the value that is passed into method m and that is the value returned by method m and assigned to element zero of the array a. As a side effect of the postfix increment expression, the value of variable i is two after method m is invoked.  
4c  Prints: 162  The compound assignment operator is right associative so the expression is evaluated from right to left. The result is a String value that can be calculated using the following expression. "1"+(2*(3+(4*(5+2))))  
5c  Prints: 1,2,3,4,5,3  Java evaluates expressions from left to right while respecting operator precedence. (1 | (2 & (3 + (4 % 5)))), (1 | (2 & (3 + (4)))), (1 | (2 & (7)))), (1 | 2)=3  
6b  Prints: [A,1],[B,1],[C,1],[D,2],2  The statement contains a series of addition operations. The left operand of each addition operator is evaluated completely before the right hand operand. The left hand operand of the first addition operator is the return value of method m1. A prefix increment expression is passed to m1 as an input parameter. The result of the pretfix expression is the original value of i plus one. The right hand operand of the addition operator is the return value of method m2. Method m2 prints the current value of i which is one and then m2 always returns zero. The result of the first addition operation becomes the left operand of the second addition operator. The right operand is the return value of method m1. The input parameter of m1 is a postfix increment expression. The result of the postfix increment expression is the existing value of i which is one and that is the value that is passed to m1 as the input parameter. The side effect of the postfix increment expression is the addition of one to the value of i. Therefore, parameter i has the value of two after method m1 is invoked. The result of the second addition operation is the value 2. The result of the second addition operation is the left hand operand of the third addition operation. Since method m2 always returns zero, the third addition operation does not change the final result which is two.  
7b  Prints: 1,1,1,0,3  The statement contains a series of addition operations. The left operand of each addition operator is evaluated completely before the right hand operand. The left hand operand of the first addition operator is a postfix increment expression that increments variable i. The result of the postfix expression is the original value of i. The side effect of the postfix operation is the addition of one to the value of variable i. The right hand operand of the addition operator is the return value of method m. Method m prints the current value of i which is one. Method m then adds the value of i to the value of x. The result is x equals one. Then m returns zero. The result of the addition operation is the sum of the original value of i which is zero and the return value of m which is also zero. The left hand operand of the next addition operator is the result of the first addition. The value of the right operand is the result of the postfix expression that increments the value of variable j. The result of the addition operation is then the sum of zero and the original value of j which is also zero. The rest of the expression is evaluated similarly. The final result of the statement is that the value of zero is assigned to variables i, j, and k. The value of x is sum of the side effects of each postfix expression.  
8a  Prints: [a,0],[b,1],[c,2],2,2,2  The question contains a series of simple assignment operators where all of the operands are array access expressions. Starting from the left, the array index expression is evaluated before the right hand operand of the simple assignment operator. Evaluation of the array index expression causes method m to be invoked. The input parameter is the postfix increment expression with the operand i. Since the postfix increment expression returns the original value of the operand, the value zero is passed to method m along with a String containing the character a. Method m returns the value that was passed in as an input parameter. In this case, the return value is zero and method m prints [a,0]. As a side effect of the postfix increment expression, the value of variable i is one after the array index expression is evaluated. The right hand operand of the first simple assignment operator is evaluated next. The right hand operand is another array access expression similar to the first. The only difference is that the value of variable i is now one greater than it was at the time when the previous array access expression was evaluated. This time, method m returns one and prints [b,1]. The final array access expression is evaluated next. Method m returns 2 and prints [c,2].  
9b  Prints: 1,2,3,1,2,3,1  Java evaluates expressions from left to right while respecting operator precedence. (1 | (2 & (3 >> ((1 / 2) + 3)))), (1 | (2 & (3 >> ((0) + 3)))), (1 | (2 & (3 >> (3)))), (1 | (2 & (0))), (1 | (0))=1  
10a  Prints: 2, 2, -3, -4, 8,  Integral primitives are stored in two's compliment format. To change a positive value into a negative value, invert each bit and add one to the result. The "~" operator is the bitwise compliment operator. It inverts the value of each bit. For example, ~0=-1.  
11c  Prints: 1  The following statements are all equivalent. i += ~i - -i * ++i + i-- % ++i * i++; Change the compound assignment operator to a simple assignment operator. i = (int) ((i) + (~i - -i * ++i + i-- % ++i * i++)); Evaluate the unary operators. i = (int) ((1) + (-2 - (-1) * 2 + 2 % 2 * 2)); Evaluate the binary operators. i = (int) ((1) + ((-2 - ((-1) * 2)) + (2 % 2 * 2))); i = (int) ((1) + ((0) + (0)));  

Copyright © 2002, Dan Chisholm
All rights reserved.
1